home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12297 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.3 KB  |  88 lines

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Help: 2 short functions
  5. Date: 29 Mar 1996 22:11:12 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4jija0INNqnn@keats.ugrad.cs.ubc.ca>
  8. References: <4ji734$n6v@masala.cc.uh.edu>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4ji734$n6v@masala.cc.uh.edu>,
  12. odie garfield <st7jr@Rosie.UH.EDU> wrote:
  13.  >Hi, I have two functions that I'm not too sure about.
  14.  >The first one:
  15.  >
  16.  >char *get_filename(void)
  17.  >{
  18.  >   char string[20],*stringp;
  19.  >   scanf("%s",string);
  20.  
  21. should be:  scanf("%19s",string);
  22.  
  23. Watch for buffer overflows, they are evil!
  24.  
  25.  >   stringp=&string[0];
  26.  
  27. This is equivalent to writing:  stringp=string
  28.  
  29.  >   return stringp;
  30.  
  31. This is equivalent, therefore, to:  return string;
  32.  
  33. And it is extremely illegal. The string object ceases to exist when the
  34. function exits. It is local to the function.
  35.  
  36.  >}
  37.  >I'm want the function to read in a string and return a pointer to that string.
  38.  >Am I doing it correctly?
  39.  
  40. No. You must either declare the string array static, or dynamically allocate it
  41. with malloc():
  42.  
  43.     static string[20];
  44.  
  45.     OR
  46.  
  47.     char *string = malloc(20);
  48.  
  49. The pitfall of declaring it static is that then it names only one object. Each
  50. time you call the function, string is the same object with the same address.
  51. You overwrite the previous contents each time you call scanf. This is fine as
  52. long as the caller understands this and takes care to copy the string to a safe
  53. place.
  54.  
  55. The pitfalls of using dynamic allocation is that you have to watch out for
  56. memory leaks and other problems. Someone has to free() the storage once it is
  57. not needed anymore.
  58.  
  59. By the way, a good design is to let the _caller_ specify the storage buffer:
  60.  
  61. void get_filename(char *buffer, int bufsize)
  62.  
  63. {
  64.     fgets(buffer, bufsize, stdin);
  65. }
  66.  
  67.  >My second function is related to the first function:
  68.  >
  69.  >FILE *open_file(char *filenamep)
  70.  >{
  71.  >   FILE *fp;
  72.  >   fp=fopen("??????","r");
  73.  >   return fp;
  74.  >}
  75.  
  76. That could be rewritten:  { return fopen(filenamep, "r"); }
  77.  
  78. It is a valid, though useless wrapper for fopen(). 
  79.  
  80.  >In this 2nd function, I want to open a file using the pointer to a string I
  81.  >got from the first function. But I don't know exactly how to do this. Any help
  82.  >would be appreciated. Thanks.
  83.  
  84. As I wrote above.
  85.  
  86. -- 
  87.  
  88.